﻿Public Class FrmKho
    Dim provider As New DataProvider
    Dim flag As String = 0
    Dim sql As String

    Private Sub FrmKho_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadKho()
    End Sub
    Private Sub LoadKho()
        sql = "SELECT k.MaKho, k.TenKho , nv.TenNV  FROM Kho k,NhanVien nv WHERE k.MaNV = nv.MaNV"
        txtmakho.Enabled = False
        txttenkho.Enabled = False
        cbxnhanvien.Enabled = False
        dtgkho.DataSource = provider.ExecuteQuery(sql)
        btnluu.Enabled = False
        btnhuy.Enabled = False
        btnthem.Enabled = True
        btnsua.Enabled = True

        sql = "select * from NhanVien"
        cbxnhanvien.DataSource = provider.ExecuteQuery(sql)
        cbxnhanvien.ValueMember = "MaNV"
        cbxnhanvien.DisplayMember = "TenNV"
    End Sub

    Private Sub dtgkho_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgkho.CellClick
        Dim row As Integer

        row = dtgkho.CurrentCell.RowIndex

        txtmakho.Text = dtgkho.Rows(row).Cells(0).Value
        txttenkho.Text = dtgkho.Rows(row).Cells(1).Value
        cbxnhanvien.Text = dtgkho.Rows(row).Cells(2).Value


    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        btnluu.Enabled = True
        btnhuy.Enabled = True
        txtmakho.Enabled = True
        txttenkho.Enabled = True
        cbxnhanvien.Enabled = True
        txtmakho.Text = ""
        txttenkho.Text = ""
        btnsua.Enabled = False
    End Sub

    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadKho()
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click

        If txtmakho.Text = "" Then
            MessageBox.Show("Chọn 1 kho để sửa")
        Else
            flag = 2
            btnluu.Enabled = True
            btnhuy.Enabled = True
            txtmakho.Enabled = False
            txttenkho.Enabled = True
            cbxnhanvien.Enabled = True
            btnthem.Enabled = False
        End If

       
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        If flag = 1 Then
            ' thêm
            Dim makho As String
            Dim tenkho As String
            Dim manv As String
            makho = txtmakho.Text
            tenkho = txttenkho.Text
            manv = cbxnhanvien.SelectedValue

            Dim dt As New DataTable
            sql = "select * from kho where MaNV='" + manv + "'"
            dt = provider.ExecuteQuery(sql)
            If dt.Rows.Count > 0 Then
                MessageBox.Show("Mã Kho đã tồn tại")
            Else
                sql = "INSERT INTO Kho(MaKho,TenKho,MaNV)VALUES('" + +"',N'" + tenkho + "','" + manv + "')"
                provider.ExecuteNonQuery(sql)
                MessageBox.Show("Thêm thành công")
            End If


        ElseIf flag = 2 Then
            Dim makho As String
            Dim tenkho As String
            Dim manv As String
            makho = txtmakho.Text
            tenkho = txttenkho.Text
            manv = cbxnhanvien.SelectedValue

            sql = "UPDATE Kho SET	TenKho = N'" + tenkho + "',	MaNV = '" + manv + " WHERE MaKho ='" + makho + "'"
        End If
    End Sub
End Class